package com.ruoyi.wvp.task;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.service.ISysConfigService;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 蒋翔 SY100242
 * @date 2024/7/22 9:22
 * @email SY100242@hnlens.com
 */
@Component("deviceOfflineMaxTask")
@Slf4j
public class DeviceOfflineMaxTask {
    private static final int MAX = 30;
    private static final int MAX_MINUTES = 21; //大于21分钟才通知

    private static LocalDateTime EXECUTE_TIME;

    public static final String CUSTOM_ROBOT_TOKEN = "9fdd794d5bcbb8d2b560bc0968fbebd8b4ba038e3589c1a67c56958f599ab8c7";

    public static final List<String> userIds = Arrays.asList(
            "1rb_zqnmuu25n1"
            , "1cl_wgbh4cz6m9"
            , "zetshl3"
            , "zvz-zjztgq04d"
            , "zrov2c7"
            , "dvy_hgmeelzis"
            , "7vs_1xa2zg47i"
    );

    public static final ExecutorService executorService = Executors.newSingleThreadExecutor();

    public void warning(List<String> offlineDevices ) {
        executorService.submit(()->{
            notifyWarning(offlineDevices);
        });
    }

    public void notifyWarning(List<String> offlineDevices ) {
        ISysConfigService bean = SpringUtils.getBean(ISysConfigService.class);
        int minMinutes= MAX_MINUTES;
        int minDevice= MAX;
        try {
            String minutes = bean.selectConfigByKey("offline.min.minutes");
            if(StringUtils.isNotBlank(minutes)){
                minMinutes=Integer.parseInt(minutes);
            }
            String minDeviceSize = bean.selectConfigByKey("offline.device.min");
            if(StringUtils.isNotBlank(minDeviceSize)){
                minDevice=Integer.parseInt(minDeviceSize);
            }
        }catch (Exception e){

        }
        if(offlineDevices.size() < minDevice){
            return;
        }

        log.info("发送通知消息消息");
        if (EXECUTE_TIME == null || EXECUTE_TIME.isBefore(LocalDateTime.now())) {
            try {
                EXECUTE_TIME = LocalDateTime.now().plusMinutes(minMinutes);
                //sign字段和timestamp字段必须拼接到请求URL上，否则会出现 310000 的错误信息
                DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send");
                OapiRobotSendRequest req = new OapiRobotSendRequest();
                /**
                 * 发送文本消息
                 */
                //定义文本内容
                OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
                text.setContent("天天创科:摄像头离线数量已经达到"+offlineDevices.size());
                //定义 @ 对象
                OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
                at.setAtUserIds(userIds);
                //设置消息类型
                req.setMsgtype("text");
                req.setText(text);
                req.setAt(at);
                OapiRobotSendResponse rsp = client.execute(req, CUSTOM_ROBOT_TOKEN);
                System.out.println(rsp.getBody());
            } catch (ApiException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }


}
